/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quizzempire.model.statemachine;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import quizzempire.model.Model;
import quizzempire.model.Question;

/**
 *
 * @author Paulo
 */
public class QuestionEditor extends State {
    
    ArrayList<Question> questions;

    public QuestionEditor(Model modelo) {
        super(modelo);
        questions=modelo.getActiveQuizz().getQuestions();
    }

    @Override
    public State back() {
        modelo.setChanges();
        return new QuizzConfiguration(modelo);
    }

    @Override
    public State addQuestion(String name) {
        Question question=new Question(name);
        questions.add(question);
        modelo.setChanges();
        return this;
    }

    @Override
    public State removeQuestion(int idQuestion) {
        if(idQuestion<0 || idQuestion>=questions.size())
            return this;
        questions.remove(idQuestion);
        modelo.setChanges();
        return this;
    }

    @Override
    public State setQuestionDifficulty(int idQuestion, int difficulty) {
        if(idQuestion<0 || idQuestion>=questions.size())
            return this;
        questions.get(idQuestion).setDifficulty(difficulty);
        return this;
    }

    @Override
    public State setQuestionText(int idQuestion, String text) {
        if(idQuestion<0 || idQuestion>=questions.size())
            return this;
        questions.get(idQuestion).setText(text);
        return this;
    }

    @Override
    public State setQuestionAnswers(int idQuestion, String[] answers) {
        if(idQuestion<0 || idQuestion>=questions.size())
            return this;
        questions.get(idQuestion).setAnswers(answers);
        return this;
    }

    @Override
    public State saveQuizz(String location) {
        
        File destination = new File(location);
        modelo.setChanges();
        if (destination.exists())
            return this;
        
        try {
            destination.createNewFile();
            
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(destination));
            oos.writeObject(modelo.getActiveQuizz()); 
            oos.close();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(QuestionEditor.class.getName()).log(Level.SEVERE, null, ex);
            return this;
        } catch (IOException ex) {
            Logger.getLogger(QuestionEditor.class.getName()).log(Level.SEVERE, null, ex);
            return this;
        }
        
        return new AwaitsInstruction(modelo);
    }

    @Override
    public State setIndexCorrect(int idQuestion, int id) {
        if (id < 0 || id > 3 || idQuestion<0 || idQuestion>=questions.size())
            return this;
        questions.get(idQuestion).setIndexCorrect(id);
        return this;
        
    }
    
    @Override
    public State restart() {
        modelo.setChanges();
        return new AwaitsInstruction(modelo);
    }

    @Override
    public State setQuestionName(int selectedid, String text) {
        questions.get(selectedid).setName(text);
        modelo.setChanges();
        return this;
    }
 
    
}
